package ca.uwindsor.cs.deepweb.utility;

import java.io.IOException;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;

public class IndexSubsetBuilder {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		args = new String[2];
		args[0] = "H:\\indexes\\GOV2_2m";
		args[1] = "2000000";

		// TODO generate a list of document num of Lucene Index
		// args[2] =
		// "/var/research/estimation/experiment/documentanalysis/corrected/gov2_0_terms_of_digit.txt"
		// ;

		int limit = Integer.parseInt(args[1]);
		IndexWriter indexWriter;
		
		// TODO Auto-generated method stub
		if (args.length != 2) {
			System.out
					.println("java -jar LuceneIndexReader.jar indexDir wordlistFile digitlistFile");
			System.exit(0);
		}
		try {
			Analyzer luceneAnalyzer = new StandardAnalyzer();
//			indexWriter = new IndexWriter(args[3],luceneAnalyzer,true);
			
			IndexReader reader = IndexReader.open(args[0]);
			int numdocs = reader.numDocs();
			System.out.println("NumDocs = " + numdocs);
			if(numdocs<limit){
				System.exit(0);
			}
			int amount = numdocs-limit;
			Random r = new Random();
			Set<Integer> set_temp = new HashSet<Integer>();
			int index;
			int j = 0;
			while (j < amount) {
				index = r.nextInt(numdocs);
				if (!set_temp.contains(index)) {
					set_temp.add(index);
					j++;
				}
			}
			
			for(Integer i : set_temp){
				reader.deleteDocument(i);
//				indexWriter.addDocument(reader.document(i));
			}
			reader.close();
			indexWriter = new IndexWriter(args[0],luceneAnalyzer,false);
			indexWriter.optimize();
			System.out.println("optimization is completed!");
			indexWriter.close();
			// reader.document(0).
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
